
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dttung
 */
@WebServlet("/ShowTml")
public class ShowTml extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, FileNotFoundException
    {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        ServletUtil.printFistHalf(out);
        out.println("<br/>\n");
        //out.println("<strong>COMMING SOON...\n");
        //out.println("<br/>\n");
        //out.println("<br/>\n");
        out.println("<table border=\"0\" bordercolor=\"#FFFFFF\" style=\"background-color:#FFFFFF\" width=\"100%\" cellpadding=\"3\" cellspacing=\"3\">\n" +               
                    "	<tr>\n" +
                    "		<td>");
        out.println("<strong>Bảng xếp hạng TML</strong><br/><br/>");
        // Table with total yff points
        out.println("<style type=\"text/css\">\n" +
                    "table.tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}\n" +
                    "table.tftable th {font-size:12px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}\n" +
                    "table.tftable tr {background-color:#d4e3e5;}\n" +
                    "table.tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}\n" +
                    "</style>\n" +
                        "\n" +
                        "<table id=\"tfhover\" class=\"tftable\" border=\"1\">\n" +                        
                        "<tr><th>#</th><th>Team</th><th>Total YFF Points</th></tr>\n");
        
        Utils myUtil = new Utils();
        try {
            myUtil.readYffScores();
            myUtil.readTeamLinks();
            myUtil.updateTMLscores();
        } catch (URISyntaxException ex) {
            Logger.getLogger(ShowTml.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        TreeMap sortedTML = myUtil.getSortedTMLTeamsOnTotalYFF();
        Iterator it = sortedTML.entrySet().iterator();
        int i = 0;
        
        while (it.hasNext())
        {
            i++;
            Map.Entry pairs = (Map.Entry)it.next();
            String team = (String) pairs.getKey();            
            float score = (Float) pairs.getValue();
            out.println("<tr><td>" + i + "</td><td>" + myUtil.getTeamLinkHtml(team,"blue") + "</td><td>" + score + "</td></tr>\n");                        
        }
        out.println("</table>"); // end total yff point table
        
        out.println("</td>\n" +
                    "		<td valign=\"top\">");
        out.println("<strong>Danh sách đoạt giải tuần TML</strong><br/><br/>");
        // Table with weekly yff points
        out.println("<style type=\"text/css\">\n" +
                    "table.tftable1 {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #9dcc7a;border-collapse: collapse;}\n" +
                    "table.tftable1 th {font-size:12px;background-color:#abd28e;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;text-align:left;}\n" +
                    "table.tftable1 tr {background-color:#bedda7;}\n" +
                    "table.tftable1 td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;}\n" +
                    "</style>\n" +
                        "<table id=\"tfhover\" class=\"tftable1\" border=\"1\">\n" +                        
                        "<tr><th>Week</th><th>Team</th><th>YFF Week Points</th></tr>\n");
        int currRound = myUtil.getCurrentRound();
        for (i = 1; i <= currRound; i++) // from week 1 to current week
        {
            ArrayList<String> teams = myUtil.getWeeklyHighestScoreTeams(i);
            String score = myUtil.getWeekYffPoint(teams.get(0), i);
            String firstTeam = teams.get(0);
            for (int j = 1; j < teams.size(); j++)
            {
                String team = teams.get(j);
                firstTeam += "&nbsp-&nbsp" + team;
            }
            out.println("<tr><td>" + i + "</td><td>" + myUtil.getTeamLinkHtml(firstTeam,"blue") + "</td><td>" + score + "</td></tr>\n");
        }
        out.println("</table>"); // end week point table
        
        // The large table that hold 2 tables of points
        out.println("</td>\n" +
                    "	</tr>\n" +
                    "</table>\n" +
                    "");
        ServletUtil.printSecondHalf(out);
        
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
}
